package com.aruba.socketlibrary.protocol.SendManager;

import android.os.HandlerThread;
import android.os.Looper;

import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Created by aruba on 2016/8/24.
 */
public class SendDataManager {
    private LinkedList<SendResponse> dataTask = new LinkedList<>();

    private static ConcurrentHashMap<String, HandlerThread> mWorkerThreadHashMap = new ConcurrentHashMap<>();

    public boolean isLock = false;

    public static Looper getWorkerLooper(String tag) {
        HandlerThread mWorkerThread = mWorkerThreadHashMap.get(tag);
        if (mWorkerThread == null) {
            mWorkerThread = new HandlerThread(tag);
            mWorkerThread.start();

            mWorkerThreadHashMap.put(tag, mWorkerThread);
        }
        return mWorkerThread.getLooper();
    }

    public void destory(String tag) {
        try {
            HandlerThread handlerThread = mWorkerThreadHashMap.get(tag);
            handlerThread.quit();

            mWorkerThreadHashMap.remove(tag);
        } catch (Exception e) {
            
        }
    }

    public LinkedList<SendResponse> getDataTask() {
        return dataTask;
    }

    public void addDataTask(SendResponse sendResponse) {
        if (dataTask.size() > 100) {
            dataTask.remove(0);
        }
        dataTask.add(sendResponse);
    }
}
